package com.pl.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pl.entity.FaultMaintenance;
import com.pl.service.FaultMaintenanceService;
import com.pl.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * (FaultMaintenance)表控制层
 *
 * @author makejava
 * @since 2024-05-03 14:38:08
 */
@RestController
@RequestMapping("/api/faultMaintenance")
public class FaultMaintenanceController {
    /**
     * 服务对象
     */
    @Autowired
    private FaultMaintenanceService faultMaintenanceService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
//    @RequestMapping("/list/page")
//    public R queryByPage(Integer pageNum) {
//        return R.ok(this.faultMaintenanceService.queryByPage(pageNum));
//    }
    @RequestMapping("/list/page")
    public R queryByPage(@RequestParam Map<String,Object> map) {

        // 1.接受参数
        System.out.println("map = " + map);

        // 2.根据参数开启分页
        int pageNum = Integer.valueOf(  (String) map.get("pageNum"));
        int pageSize =Integer.valueOf(  (String) map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);

        // 3.按照条件查询
        List<FaultMaintenance> list = faultMaintenanceService.queryByPageCondition(map);

        // 4 返回R 内部有pageinfo
        if (list!=null && list.size()>0){ // 返回成功数据

            PageInfo<FaultMaintenance> pageInfo = new PageInfo<>(list);// 目的是的到总行数 总页数

            return R.ok(pageInfo);
        }

        return R.fail("查询数据为空");


    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("/{id}")
    public R queryById(@PathVariable("id") Integer id) {
        return R.ok(this.faultMaintenanceService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param faultMaintenance 实体
     * @return 新增结果
     */
    @RequestMapping("/save")
    public R add(FaultMaintenance faultMaintenance, HttpSession session) {
        boolean result = this.faultMaintenanceService.insert(faultMaintenance, session);

        if (result){
            return R.ok();
        }

        return R.fail("新增失败");    }

    /**
     * 编辑数据
     *
     * @param faultMaintenance 实体
     * @return 编辑结果
     */
    @RequestMapping("/edit")
    public R edit( FaultMaintenance faultMaintenance) {
        return R.ok(this.faultMaintenanceService.update(faultMaintenance));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping("/del/{id}")
    public R deleteById(@PathVariable("id") Integer id) {
        return R.ok(this.faultMaintenanceService.deleteById(id));
    }

}

